package com.ioc.business.job.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.job.domain.JobNodeUseTime;
import com.ioc.business.job.domain.bo.node.use.time.JobNodeUseTimeBo;
import com.ioc.business.job.domain.bo.node.use.time.JobNodeUseTimeQueryBo;
import com.ioc.business.job.mapper.JobNodeUseTimeMapper;
import com.ioc.business.job.service.IJobNodeUseTimeService;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimeImportVo;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimePageVo;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimeVo;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.exception.ServiceException;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 作业节点用时表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-08
 */
@Service
@AllArgsConstructor
public class JobNodeUseTimeServiceImpl extends ServiceImpl<JobNodeUseTimeMapper, JobNodeUseTime> implements IJobNodeUseTimeService {

    @Override
    public TableVo<JobNodeUseTime, JobNodeUseTimePageVo> selectJobNodeUseTimePageList(JobNodeUseTimeQueryBo queryBo) {
        List<JobNodeUseTime> result = baseMapper
                .queryPageList(queryBo);
        List<JobNodeUseTimePageVo> pageVos = new ArrayList<>(result.size());
        if (CollectionUtil.isNotEmpty(result)) {
            pageVos = buildPageVos(result);
        }
        return new TableVo<>(result, pageVos);
    }

    private List<JobNodeUseTimePageVo> buildPageVos(List<JobNodeUseTime> records) {
        return records.stream().map(record -> {
            JobNodeUseTimePageVo vo = new JobNodeUseTimePageVo();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public JobNodeUseTimeVo selectJobNodeUseTimeById(Long jobNodeUseTimeId) {
        JobNodeUseTime jobNodeUseTime = getById(jobNodeUseTimeId);
        if (ObjectUtil.isNotEmpty(jobNodeUseTime)) {
            JobNodeUseTimeVo jobNodeUseTimeVo = new JobNodeUseTimeVo();
            BeanUtils.copyProperties(jobNodeUseTime, jobNodeUseTimeVo);
            return jobNodeUseTimeVo;
        }
        return null;
    }

    /**
     * 新增收货订单
     *
     * @param bo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addJobNodeUseTime(JobNodeUseTimeBo bo) {
        JobNodeUseTime jobNodeUseTime = new JobNodeUseTime();
        BeanUtils.copyProperties(bo, jobNodeUseTime);
        jobNodeUseTime.setCreateBy(SecurityUtils.getUsername());
        jobNodeUseTime.setCreateTime(new Date());
        jobNodeUseTime.setUpdateBy(SecurityUtils.getUsername());
        jobNodeUseTime.setUpdateTime(jobNodeUseTime.getCreateTime());
        jobNodeUseTime.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
        return save(jobNodeUseTime);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateJobNodeUseTime(JobNodeUseTimeBo bo) {
        JobNodeUseTime jobNodeUseTime = new JobNodeUseTime();
        BeanUtils.copyProperties(bo, jobNodeUseTime);
        jobNodeUseTime.setUpdateBy(SecurityUtils.getUsername());
        jobNodeUseTime.setUpdateTime(jobNodeUseTime.getCreateTime());
        return updateById(jobNodeUseTime);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeJobNodeUseTime(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<JobNodeUseTime> updateList = idList.stream().map(id -> {
            JobNodeUseTime jobNodeUseTime = new JobNodeUseTime();
            jobNodeUseTime.setJobNodeUseTimeId(id);
            jobNodeUseTime.setDelFlag(UserConstants.DEL_FLAG_REMOVED);
            return jobNodeUseTime;
        }).collect(Collectors.toList());
        return updateBatchById(updateList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importJobNodeUseTime(List<JobNodeUseTimeImportVo> voList) {
        List<JobNodeUseTime> jobNodeUseTimeList = verifyImportData(voList);
        saveBatch(jobNodeUseTimeList);
    }

    private List<JobNodeUseTime> verifyImportData(List<JobNodeUseTimeImportVo> voList) {
        if (CollectionUtil.isEmpty(voList)) {
            throw new ServiceException("导入数据不能为空");
        }
        List<JobNodeUseTime> jobNodeUseTimeList = new ArrayList<>(voList.size());
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        for (JobNodeUseTimeImportVo jobNodeUseTimeImportVo : voList) {
            JobNodeUseTime jobNodeUseTime = new JobNodeUseTime();
            BeanUtils.copyProperties(jobNodeUseTimeImportVo, jobNodeUseTime);
            verifyField(jobNodeUseTime, jobNodeUseTimeImportVo);
            jobNodeUseTime.setCreateBy(username);
            jobNodeUseTime.setCreateTime(date);
            jobNodeUseTime.setUpdateBy(username);
            jobNodeUseTime.setUpdateTime(date);
            jobNodeUseTime.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
            jobNodeUseTimeList.add(jobNodeUseTime);
        }
        return jobNodeUseTimeList;
    }

    private void verifyField(JobNodeUseTime jobNodeUseTime, JobNodeUseTimeImportVo jobNodeUseTimeImportVo) {
        String jobNodeDateStr = jobNodeUseTimeImportVo.getJobNodeDate();
        Date jobNodeDate = null;
        try {
            jobNodeDate = StrUtil.isNotBlank(jobNodeDateStr) ? DateUtil.parse(jobNodeDateStr) : null;
        } catch (Exception e) {
            throw new ServiceException("日期格式不正确!");
        }
        jobNodeUseTime.setJobNodeDate(jobNodeDate);
    }
}
